package com.egg.ylt.activity;

import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.presenter.impl.NewPasswordPresenterImpl;
import com.egg.ylt.view.INewPasswordView;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.MD5Util;
import com.yonyou.framework.library.common.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_SetNewPassword extends BaseActivity<NewPasswordPresenterImpl> implements INewPasswordView {


    @BindView(R.id.include_iv_back)
    ImageView includeIvBack;
    @BindView(R.id.include_tv_title)
    TextView includeTvTitle;
    @BindView(R.id.include_tv_right)
    TextView includeTvRight;
    @BindView(R.id.include_rl_view)
    RelativeLayout includeRlView;
    @BindView(R.id.tv_phone_new)
    TextView tvPhoneNew;
    @BindView(R.id.edit_pwd_new)
    EditText editPwdNew;
    @BindView(R.id.tv_password_show)
    ImageView tvPasswordShow;
    @BindView(R.id.btn_next_new)
    TextView btnNextNew;
    @BindView(R.id.target_view_ll)
    LinearLayout llTargetView;

    private String mPhone;
    private boolean mPasswordType = false;

    public static int USER_SETTING_TO_NEW_PASSWORD = 0;
    public static int ACCOUNT_PASSWORD_TO_NEW_PASSWORD = 1;
    private int type = -15;

    @Override
    protected void getBundleExtras(Bundle extras) {
        mPhone = extras.getString("phone");
        type = extras.getInt("type");
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_set_new_password;
    }

    @Override
    protected View getLoadingTargetView() {
        return llTargetView;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, includeRlView);
        //设置title
        includeTvTitle.setText("设置新密码");

        //显示手机号
        tvPhoneNew.setText("请为您的账号 " + CustomUtils.setPhoneHide(mPhone) + " 设置一个新密码");
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @OnClick({R.id.include_iv_back, R.id.tv_password_show, R.id.btn_next_new})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.include_iv_back://返回键
                finish();
                break;
            case R.id.tv_password_show://显示  隐藏密码
                if (mPasswordType) {
                    editPwdNew.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    tvPasswordShow.setImageResource(R.mipmap.ic_login_pwd_hide);
                    mPasswordType = false;
                } else {
                    editPwdNew.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    tvPasswordShow.setImageResource(R.mipmap.ic_login_pwd_show);
                    mPasswordType = true;
                }
                //设置光标在内容的末尾
                editPwdNew.setSelection(editPwdNew.getText().toString().length());
                break;
            case R.id.btn_next_new://下一步
                String newPassword = editPwdNew.getText().toString().trim();
                if (StringUtil.isEmpty(newPassword)) {
                    ToastUtil.makeText(mContext, "新密码为空", false);
                    return;
                }
                if (newPassword.length() < 6) {
                    ToastUtil.makeText(mContext, "请设置密码长度在6-12位", false);
                    return;
                }
                mPresenter.savePassword(mPhone, MD5Util.string2MD5(newPassword), Constants.COMPANYID);
                break;
        }
    }

    @Override
    public void saveNewPassword() {
        //去账号 密码登录界面
        if (type == Constants.FIND_PASSWORD_TO_CODE) {
            readyGoThenKill(ACT_AccountPasswordLogin.class);
        } else if (type == Constants.UPDATE_PHONE_TO_CODE) {
            readyGo(ACT_HomePage.class);
        }
    }
}
